using System;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace RealDolmen.Helpers
{
    public enum OnNullMode
    {
        LeaveToken,
        EmptyString,
        CustomFiller
    }
    public class TextParser
    {
        #region Properties
        private OnNullMode onNullMode = OnNullMode.LeaveToken;

        public OnNullMode OnNullMode
        {
            get { return onNullMode; }
            set { onNullMode = value; }
        }

        private string customFiller;

        public string CustomFiller
        {
            get { return customFiller; }
            set { 
                customFiller = value;
                OnNullMode = OnNullMode.CustomFiller;
            }
        } 
        #endregion // Properties

        #region Constructor
        protected TextParser()
        {
        } 
        #endregion // Constructor

        #region Statics
        public static TextParser Default
        {
            get
            {
                return new TextParser();
            }
        } 
        #endregion // Statics

        #region Business Method
        public string ParseText(string original, IStringDictionary dictionary)
        {
            if (original == null)
                return null;

            string temp = original;
            StringBuilder result = new StringBuilder();
            StringBuilder currentStringBuilder = result;

            for (int i = 0; i < temp.Length; i++)
            {
                char current = temp[i];

                if (current == '[')
                {
                    currentStringBuilder = new StringBuilder();
                }
                else if (current == ']')
                {
                    string value = null;
                    string keyword = currentStringBuilder.ToString();
                    string whole = string.Format("{0}[{1}]{2}", result, keyword, temp.Substring(i + 1));

                    List<string> attributes = new List<string>(keyword.Split(','));
                    if (attributes.Count > 1)
                    {
                        keyword = attributes[0];
                        attributes.RemoveAt(0);
                    }

                    int prefixDelimiter = keyword.IndexOf(':');

                    if (prefixDelimiter > 0)
                    {
                        string keyword2 = keyword.Substring(prefixDelimiter + 1);
                        string prefix2 = keyword.Remove(prefixDelimiter);

                        value = GetString(dictionary, keyword2, whole, attributes, prefix2);
                    }
                    else
                        value = GetString(dictionary, keyword, whole, attributes);

                    if (value != null)
                        result.Append(value);
                    else
                    {
                        if (OnNullMode == OnNullMode.LeaveToken)
                            result.AppendFormat("[{0}]", keyword);
                        else if (OnNullMode == OnNullMode.CustomFiller)
                            result.Append(CustomFiller);
                    }

                    currentStringBuilder = result;
                }
                else
                    currentStringBuilder.Append(current);
            }

            return result.ToString();
        } 
        #endregion // Business Method

        #region Virtual Methods
        protected virtual string GetString(IStringDictionary dictionary, string key, string whole, List<string> attributes, string prefix)
        {
            if (prefix == "d")
                return DateTime.Now.ToString(key);
            else
                return dictionary.GetString(key, whole, attributes, prefix);
        }

        protected virtual string GetString(IStringDictionary dictionary, string key, string whole, List<string> attributes)
        {
            return dictionary.GetString(key, whole, attributes);
        } 
        #endregion // Virtual Methods

        #region Static Helpers
        public static string RemoveBlock(string format, string block, bool removeContent)
        {
            if (removeContent)
            {
                string pattern = string.Format(@"\[{0}\].*\[\/{0}\]", block);

                return Regex.Replace(format, pattern, string.Empty);
            }
            else
            {
                format = format.Replace(string.Format("[{0}]", block), string.Empty);
                format = format.Replace(string.Format("[/{0}]", block), string.Empty);
                return format;
            }
        } 
        #endregion // Static Helpers
    }
}
